﻿
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Identity.Client;
using System.IO;
using System.Threading.Tasks;
using TabbedTemplate.Services;
using Xamarin.Forms;

namespace TabbedTemplate.Droid
{
    [Activity(Label = "食记", Icon = "@drawable/app_icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        internal static MainActivity Instance { get; private set; }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            //注册syncfusion的licenseKey
            Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("@31392e312e30iELNMAh77p70EV/URzFUZYbeKezI9nfo600qndpMpJQ=");
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            Instance = this;
            SimpleIoc.Default.Register<IPhotoPickerService, PhotoPickerService>();
            SimpleIoc.Default.Register<IImageService, ImageService>();
            DependencyService.Register<ImageService>();
            SimpleIoc.Default.Register<IDrawPicService, DrawPicService>();
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            Forms.Init(this, savedInstanceState);
            base.OnCreate(savedInstanceState);
            Rg.Plugins.Popup.Popup.Init(this);
            LoadApplication(new App());
            Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
            App.AuthUIParent = this;
            DependencyService.Get<ImageService>().SetActivity(this);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        }

        public static readonly int PickImageId = 1000;

        public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
        {
            base.OnActivityResult(requestCode, resultCode, intent);
            AuthenticationContinuationHelper
                .SetAuthenticationContinuationEventArgs(requestCode, resultCode, intent);
            if (requestCode == PickImageId)
            {
                if ((resultCode == Result.Ok) && (intent != null))
                {
                    Android.Net.Uri uri = intent.Data;
                    Stream stream = ContentResolver.OpenInputStream(uri);

                    // Set the Stream as the completion of the Task
                    PickImageTaskCompletionSource.SetResult(stream);
                }
                else
                {
                    PickImageTaskCompletionSource.SetResult(null);
                }
            }

            if (requestCode == 0)
            {
                AuthenticationContinuationHelper
                    .SetAuthenticationContinuationEventArgs(requestCode, resultCode,
                        intent);
            }

        }
    }
}
